package com.acgist.polaris;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * <p>常用工具</p>
 * 
 * @author acgist
 */
public final class Tools {

	/**
	 * <p>禁止创建实例</p>
	 */
	private Tools() {
	}
	
	/**
	 * <p>字节数组转为long</p>
	 * 
	 * @param bytes 字节数组
	 * 
	 * @return long
	 */
	public static final long bytesToLong(byte[] bytes) {
		long value = 0L;
		value |= (bytes[0] & 0xFF);
		value <<= 8;
		value |= (bytes[1] & 0xFF);
		value <<= 8;
		value |= (bytes[2] & 0xFF);
		value <<= 8;
		value |= (bytes[3] & 0xFF);
		value <<= 8;
		value |= (bytes[4] & 0xFF);
		value <<= 8;
		value |= (bytes[5] & 0xFF);
		value <<= 8;
		value |= (bytes[6] & 0xFF);
		value <<= 8;
		value |= (bytes[7] & 0xFF);
		return value;
	}
	
	/**
	 * <p>long转为字节数组</p>
	 * 
	 * @param value long
	 * 
	 * @return 字节数组
	 */
	public static final byte[] longToBytes(long value) {
		final byte[] bytes = new byte[8];
		bytes[0] = (byte) ((value >> 56) & 0xFF);
		bytes[1] = (byte) ((value >> 48) & 0xFF);
		bytes[2] = (byte) ((value >> 40) & 0xFF);
		bytes[3] = (byte) ((value >> 32) & 0xFF);
		bytes[4] = (byte) ((value >> 24) & 0xFF);
		bytes[5] = (byte) ((value >> 16) & 0xFF);
		bytes[6] = (byte) ((value >> 8) & 0xFF);
		bytes[7] = (byte) (value & 0xFF);
		return bytes;
	}
	
	/**
	 * <p>字节数组转为int</p>
	 * 
	 * @param bytes 字节数组
	 * 
	 * @return int
	 */
	public static final int bytesToInt(byte[] bytes) {
		int value = 0;
		value |= (bytes[0] & 0xFF);
		value <<= 8;
		value |= (bytes[1] & 0xFF);
		value <<= 8;
		value |= (bytes[2] & 0xFF);
		value <<= 8;
		value |= (bytes[3] & 0xFF);
		return value;
	}
	
	/**
	 * <p>int转为字节数组</p>
	 * 
	 * @param value int
	 * 
	 * @return 字节数组
	 */
	public static final byte[] intToBytes(int value) {
		final byte[] bytes = new byte[4];
		bytes[0] = (byte) ((value >> 24) & 0xFF);
		bytes[1] = (byte) ((value >> 16) & 0xFF);
		bytes[2] = (byte) ((value >> 8) & 0xFF);
		bytes[3] = (byte) (value & 0xFF);
		return bytes;
	}
	
	/**
	 * <p>字节数组转为short</p>
	 * 
	 * @param bytes 字节数组
	 * 
	 * @return short
	 */
	public static final short bytesToShort(byte[] bytes) {
		short value = 0;
		value |= (bytes[0] & 0xFF);
		value <<= 8;
		value |= (bytes[1] & 0xFF);
		return value;
	}
	
	/**
	 * <p>short转为字节数组</p>
	 * 
	 * @param value short	
	 * 
	 * @return 字节数组
	 */
	public static final byte[] shortToBytes(short value) {
		final byte[] bytes = new byte[2];
		bytes[0] = (byte) ((value >> 8) & 0xFF);
		bytes[1] = (byte) (value & 0xFF);
		return bytes;
	}
	
	/**
	 * <p>文件大小单位</p>
	 */
	private static final String[] FILE_LENGTH_UNIT = {"B", "KB", "M", "G", "T"};
	
	/**
	 * <p>文件大小格式化</p>
	 * 
	 * @param size 文件大小
	 * 
	 * @return 文件大小格式化字符串
	 */
	public static final String formatSize(Long size) {
		if(size == null || size == 0L) {
			return "0B";
		}
		int index = 0;
		BigDecimal decimal = new BigDecimal(size);
		while(decimal.longValue() >= Config.DATA_SCALE) {
			if(++index >= FILE_LENGTH_UNIT.length) {
				index = FILE_LENGTH_UNIT.length - 1;
				break;
			}
			decimal = decimal.divide(new BigDecimal(Config.DATA_SCALE));
		}
		return decimal.setScale(2, RoundingMode.HALF_UP) + FILE_LENGTH_UNIT[index];
	}
	
}
